package com.qing.cheng.service.impl;

import com.qing.cheng.dto.QcCourseDto;
import com.qing.cheng.dto.QcCourseStateDto;
import com.qing.cheng.entity.QcClass;
import com.qing.cheng.entity.QcCourse;
import com.qing.cheng.entity.QcCourseState;
import com.qing.cheng.entity.QcSchool;
import com.qing.cheng.mapper.QcClassMapper;
import com.qing.cheng.mapper.QcCourseMapper;
import com.qing.cheng.mapper.QcCourseStateMapper;
import com.qing.cheng.mapper.QcSchoolMapper;
import com.qing.cheng.service.QcCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class QcCourseServiceImpl implements QcCourseService {

    @Autowired(required = false)
    private QcCourseMapper qcCourseMapper;

    @Autowired(required = false)
    private QcSchoolMapper qcSchoolMapper;
    @Autowired(required = false)
    private QcClassMapper qcClassMapper;

    @Autowired(required = false)
    private QcCourseStateMapper qcCourseStateMapper;

    @Override
    public QcCourseDto getOneById(Integer id) {
        QcCourse qcCourse = qcCourseMapper.selectByPrimaryKey(id);
        QcCourseDto qcCourseDto = new QcCourseDto();
        BeanUtils.copyProperties(qcCourse,qcCourseDto);
        List<QcCourseStateDto> courseStateDtoList = qcCourseStateMapper.getAllStateByCourseId(qcCourse.getId());
        QcClass qcClass = qcClassMapper.selectById(qcCourse.getClassId());
        QcSchool qcSchool = qcSchoolMapper.selectById(qcClass.getSchoolId());
        qcCourseDto.setQcClass(qcClass);
        qcCourseDto.setQcSchool(qcSchool);
        qcCourseDto.setQcCourseStateList(courseStateDtoList);
        //TODO 获取地址信息
        return qcCourseDto;
    }

    @Override
    public List<QcCourseDto> listAllByClassId(Integer classId) {
        List<QcCourseDto> qcCourseDtos = new ArrayList<>();
        List<QcCourse> courseList = qcCourseMapper.listAllCourseByClassId(classId);
        for (QcCourse course : courseList) {
            QcCourseDto courseDto = new QcCourseDto();
            BeanUtils.copyProperties(course,courseDto);
            courseDto.setQcCourseStateList(qcCourseStateMapper.getAllStateByCourseId(course.getId()));
            // TODO 地址设置
            courseDto.setAddress(null);
            qcCourseDtos.add(courseDto);
        }
        return qcCourseDtos;
    }

    @Override
    public void publishingCourses(QcCourseDto courseDto) {
        QcCourse course = new QcCourse();
        BeanUtils.copyProperties(courseDto,course);
        course.setAddressId(courseDto.getAddress().getId());
        course.setClassId(courseDto.getQcClass().getId());
        course.setSchoolName(courseDto.getQcSchool().getSchoolName());
        course.setClassName(courseDto.getQcClass().getClassName());
        qcCourseMapper.insert(course);
        List<QcCourseStateDto> qcCourseStateList = courseDto.getQcCourseStateList();
        for (QcCourseStateDto stateDto : qcCourseStateList) {
            QcCourseState courseState = new QcCourseState();
            BeanUtils.copyProperties(stateDto,courseState);
            courseState.setCourseId(course.getId());
            qcCourseStateMapper.insertSelective(courseState);
        }
    }

}
